package view;


import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.Socket;

import commands.AddFileCommand;
import commands.AddFolderCommand;
import commands.DeleteFileCommand;
import commands.DeleteFolderCommand;
import commands.ModifyFileCommand;

import clientRequest.ClientRequest;

public class MyProtocol implements Runnable{
	 
	ObjectInputStream in; PrintWriter out;
	Socket clientSocket;
	 
	public MyProtocol(ObjectInputStream in, PrintWriter out, Socket clientSocket){
		this.in=in; this.out=out;
		this.clientSocket = clientSocket;
	 }
	
	public void run(){
		
		//String request="",response="";
		AddFileCommand addFileCommand = new AddFileCommand();
		DeleteFileCommand deleteFileCommand = new DeleteFileCommand();
		ModifyFileCommand modifyFileCommand = new ModifyFileCommand();
		AddFolderCommand addFolderCommand = new AddFolderCommand();
		DeleteFolderCommand deleteFolderCommand = new DeleteFolderCommand();
		ClientRequest clientRequest;
		
		try {
			
			
			//clientString = stringIn.readLine();
			//System.out.println(clientString);
			
			/*try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
			
			clientRequest = (ClientRequest) in.readObject();
			clientRequest.printRequest();
			//System.out.println("after");
			//System.out.println(clientRequest);
			
			//System.out.println("kind :::: " + clientRequest.getKindRequest());
			
			while ((clientRequest!=null) && (!clientRequest.getKindRequest().equals("bye")))
			{
				if (clientRequest.getKindRequest().equals("ENTRY_CREATE"))
				{
					
					//System.out.println("create");
					if (!clientRequest.isFolder())
						addFileCommand.action(clientRequest);
					else
						addFolderCommand.action(clientRequest);
				}
				else if (clientRequest.getKindRequest().equals("ENTRY_DELETE"))
				{
					//System.out.println("delete");
					deleteFileCommand.action(clientRequest);
					deleteFolderCommand.action(clientRequest);
						
				}
				else if (clientRequest.getKindRequest().equals("ENTRY_MODIFY"))
				{
					//System.out.println("modify");
					if (!clientRequest.isFolder())
						modifyFileCommand.action(clientRequest);				
				}
				
				//treat the req

				/*try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}*/
				
				clientRequest = (ClientRequest) in.readObject();
				clientRequest.printRequest();
			}
						
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("my pr");
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("my pr");
			e.printStackTrace();
		}
		finally {
			//try {				
				
				//out.println("bye");
			
			if (out!=null)				
				out.close();
			
			try {
				if (in!=null)
					in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			try {
				if (clientSocket!=null)
					clientSocket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
							
			/*} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
		}

		
		/*try {
			while(!request.startsWith("Exit")){
				request=in.readLine();
				boolean result=interpreter.updateClientRequest(in);
				if(result==true)
					response="DONE";
				else
					response="FAILED";
				out.println(response);
			  
			}
			//out.println("Bye");
	  }catch (IOException e) {} */
	}

}